package com.example.controller;

import com.example.annotation.Login;
import com.example.dto.AccountDto;
import com.example.dto.MenuDto;
import com.example.exception.MyException;
import com.example.service.MenuService;
import com.example.service.UserService;
import com.example.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    /*
     * 用于主页查询用户可以查看的菜单
     * */
    @Login
    @RequestMapping("/selectUserInfo")
    public Result selectUserInfo(AccountDto accountDto) {
        try {
            List<MenuDto> menuList = userService.selectMenuByUserId(accountDto.getId());
            return Result.success().put("user", accountDto).put("menuList", menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    //    发送短信
    @RequestMapping("sendMsg/{phoneCode}")
    public Result sendMsg(@PathVariable("phoneCode") String phoneCode) {
        System.out.println("进入发短信方法");
        try {
//            发短信
            userService.sendMsg(phoneCode);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    //    登录校验
    @RequestMapping("/login")
    public Result login(@RequestBody AccountDto accountDto) {
        try {
            String token = userService.login(accountDto);
            return Result.success().put("token", token);
        } catch (MyException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }
}
